<?php

namespace common\extensions\exception;

use yii\base\ErrorHandler as BaseErrorHandler;

class H5ErrorHandler extends BaseErrorHandler {

    public $defaultName;

    public $defaultMessage;

    const STATUS_FAILED = 1;

    public function renderException($exception) {

        if ($exception === null) {
            // action has been invoked not from error handler, but by direct route, so we display '404 Not Found'
            $exception = new HttpException(404, Yii::t('yii', 'Page not found.'));
        }

        if ($exception instanceof HttpException) {
            $code = $exception->statusCode;
        } else {
            $code = $exception->getCode();
        }

        //exit(json_encode(array('code' => $exception->getCode(), 'msg' => $exception->getMessage())));

        $data = [
            'errorCode' => $code,
            'message' => $exception->getMessage(),
        ];

        $arrayFailed = \Yii::$app->utils->getFailedArray("请求失败", self::STATUS_FAILED, $data);
        header("Content-Type:application/json; charset=UTF-8");
        echo json_encode($arrayFailed);
        exit(0);
    }

}